#include <iostream>
#include "datapreprocess.h"
#include "aprioriImpl.h"
#include <dirent.h>
#include <sys/stat.h>
#include "ClusterAnalysis.h"
using namespace std;

void apriori(string s_dir, string d_dir, int min_sup, double min_con) {
    DIR *dp;
    DIR *dp1;
    struct dirent *dirp;
    struct stat filestat;
    string infilename;
    string outfilename;
    ofstream outfile;
    if ( !(dp = opendir(s_dir.c_str())) ) {
        cout << "open " << s_dir << "failed!" << endl;
        return;
    }
    if ( !(dp1 = opendir(d_dir.c_str())) ) {
        if (mkdir(d_dir.c_str(), 0755)) {
            cout << "create " << d_dir << " failed!" << endl;
            return;
        }
    }
    while ((dirp = readdir(dp))) {
        infilename = s_dir + dirp->d_name;
        //if the file is directory or invalid , skip it
        if (stat(infilename.c_str(), &filestat)) {
            continue;
        }
        if (S_ISDIR(filestat.st_mode)) {
            continue;
        }
        outfilename = d_dir + dirp->d_name + "_rules";
        outfile.open(outfilename);
        map<ItemSet, int> FreItemSupport;
        SuperItemSet L = genL1(infilename, FreItemSupport, min_sup);
        while (L.size()) {
            L = scanDB(infilename, genCk(L), FreItemSupport, min_sup);
            showRule(outfile, L, FreItemSupport, min_con);
        }
        outfile.close();
        outfile.clear();
    }
}

int main() {
    string s_dir = "/Users/likang/ClionProjects/DNS_mining/data/";
    string QVPd_dir = "/Users/likang/ClionProjects/DNS_mining/QVPdata/";
    string dns_dir = "/Users/likang/ClionProjects/DNS_mining/dnsdata/";
    QVP_data_preprocess(s_dir, QVPd_dir, dns_dir);
    string Apriori_dir = "/Users/likang/ClionProjects/DNS_mining/Association_rules/";
    apriori(QVPd_dir, Apriori_dir, 3, 0.8);
    apriori(dns_dir, Apriori_dir, 3, 0.8);
    ClusterAnalysis myClusterAnalysis;                        //聚类算法对象声明
    myClusterAnalysis.Init((char *) "/Users/likang/ClionProjects/DNS_mining/QTCAdata", 0.4, 3);        //算法初始化操作，指定半径为15，领域内最小数据点个数为3，（在程序中已指定数据维度为2）
    myClusterAnalysis.DoDBSCANRecursive();                    //执行聚类算法
    myClusterAnalysis.WriteToFile((char *) "/Users/likang/ClionProjects/DNS_mining/Result.txt");//写执行后的结果写入文件
    return 0;
}